package top.infopub.mgr.sys.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.sys.dao.HolidaysDao;
import top.infopub.mgr.sys.service.HolidaysService;
import top.infopub.mgr.sys.vo.HolidaysVo;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.sys.Holidays;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class HolidaysServiceImpl extends GenericServiceImpl<Holidays, String> implements
		HolidaysService {

	@Autowired
	private HolidaysDao holidaysDao;
	
	@Override
	public GenericDao<Holidays, String> getDao() {
		return holidaysDao;
	}

    @Override
    public Page<Holidays> selectPageByVo(Page<Holidays> page, HolidaysVo holidaysVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<Holidays>)holidaysDao.selectDataByCondition(holidaysVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initFestivalDay(String year) {
        batchDelete(year);
        int rownum = 365;
        int y = Integer.parseInt(year);
        if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0)//判断是否为闰年
        {    
            rownum = 366;   
        }
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("select to_date('").append(year).append("-01-01','yyyy-mm-dd') + (rownum - 1) from dual connect by rownum<=").append(rownum);
        List<String> dates = holidaysDao.selectListStringBysql(stringBuffer.toString());
        List<Holidays> holidays = new ArrayList<Holidays>(); 
        for(String date : dates){
            // 自动生成主键uuid
            String id = UUID.randomUUID().toString().toUpperCase().replaceAll("\\-", "");
            String strDate = date.substring(0,10);
            StringBuffer buffer = new StringBuffer();
            buffer.append("select to_char(to_date('").append(strDate).append("','yyyy-mm-dd'),'d') from dual");
            String strWeekday = holidaysDao.selectOneFieldBysql(buffer.toString());//7为星期六、1为星期日
            Holidays holiday = new Holidays();
            holiday.setId(id);
            holiday.setDcurrentdate(DateUtils.parseDate(strDate));
            holiday.setDadddate(DateUtils.getCurrentDateTime());
            if("7".equals(strWeekday) || "1".equals(strWeekday)){
                holiday.setBisworkdate(0);
            }else{
                holiday.setBisworkdate(1);
            }
            holidays.add(holiday);
        }
        //批量插入
        holidaysDao.insertByBatch(holidays);
    }

    public void batchDelete(String year){
        String beginStr = year + "-1-1 00:00:00";
        String endStr = year + "-12-31 00:00:00";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("delete from SYS_HOLIDAYS  where 1=1 ");
        stringBuffer.append("and DCURRENTDATE>=").append("to_date('").append(beginStr).append("','yyyy-mm-dd hh24:mi:ss') ")   ;
        stringBuffer.append("and DCURRENTDATE<=").append("to_date('").append(endStr).append("','yyyy-mm-dd hh24:mi:ss') ");
        holidaysDao.batchDeleteDay(stringBuffer.toString());
    }
    
    @Override
    public void setWork(String id) {
        Holidays festivalDay = new Holidays();
        festivalDay.setId(id);
        festivalDay.setBisworkdate(1);
        holidaysDao.updateByIdSelective(festivalDay);
    }

    @Override
    public void setRest(String id) {
        Holidays festivalDay = new Holidays();
        festivalDay.setId(id);
        festivalDay.setBisworkdate(0);
        holidaysDao.updateByIdSelective(festivalDay);
    }
    
}